﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Models;

public partial class Services_getInfoForModle : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/plain";
        Response.Charset = "utf-8";
        string resultJson = "";//返回结果，json格式
        //"{\"name\":\"micheal\",\"adress\":\"chaoyang Road\"}";
        //查询已有模型需要3个参数：distid-行政区编码、mdate-时间、module_typeid-模型编码
        string _distid = "";
        string _mdate = "";
        string _modelTypeid = "";
        string _model="";
        string x_type = "";
        string y_type = "";
        _distid = Request.QueryString[0].ToString();
        _mdate = Request.QueryString[1].ToString();
        string x_y = Request.QueryString[3].ToString();
        string[] x_y_arr = x_y.Split('-');
        x_type = x_y_arr[1];
        y_type = x_y_arr[0];
        _mdate=_mdate.Replace('_', '-');
        _modelTypeid = Request.QueryString[2].ToString();
        switch (_modelTypeid)
        {
            case "1": _model = "all";
                break;
            case "2": _model = "linear";
                break;
            case "3": _model = "exp";
                break;
            case "4" :_model="log";
                break;
        }
        ModelEquations m=new ModelEquations();

        if (_modelTypeid == "1")
        {
            string[] model = new string[3] { "linear", "exp", "log" };
            resultJson = "{";

            for (int i = 0; i < model.Length; i++)
            {
                _model = model[i];
                m = ModelEqManager.GetModel(_distid, _mdate, _model, x_type, y_type);
                if (m != null && i == 0)
                {
                    resultJson += "\"L_Goodness\":\"" + m.Goodness + "\"," + "\"L_Intercept\":\"" + m.Intercept + "\",\"L_Coefficient\":\"" + m.Coefficient + "\",";
                }
                else if( i==0 )
                {
                    resultJson += "\"L_Goodness\":\"0.0\",\"L_Intercept\":\"0.0\",\"L_Coefficient\":\"0.0\",";
                }
                if (m != null && i == 1)
                {
                    resultJson += "\"E_Goodness\":\"" + m.Goodness + "\"," + "\"E_Intercept\":\"" + m.Intercept + "\",\"E_Coefficient\":\"" + m.Coefficient + "\",";
                }
                else if (i == 1)
                {
                    resultJson += "\"E_Goodness\":\"0.0\",\"E_Intercept\":\"0.0\",\"E_Coefficient\":\"0.0\",";
                }
                if (m != null && i == 2)
                {
                    resultJson += "\"Lo_Goodness\":\"" + m.Goodness + "\"," + "\"Lo_Intercept\":\"" + m.Intercept + "\",\"Lo_Coefficient\":\"" + m.Coefficient + "\"";
                }
                else if (i == 2)
                {
                    resultJson += "\"Lo_Goodness\":\"0.0\",\"Lo_Intercept\":\"0.0\",\"Lo_Coefficient\":\"0.0\"";
                }
            }
            resultJson += "}";
        }
        else
        {
            m = ModelEqManager.GetModel(_distid, _mdate, _model, x_type, y_type);
            if (m != null) resultJson = "{\"Distid\":\"" + m.Distid + "\",\"Mdate\":\"" + m.Mdate + "\","
                + "\"Mtypeid\":\"" + m.Module_typeid + "\",\"Mname\":\"" + m.Module_name + "\",\"Mtype\":\"" + m.Model_type + "\","
                + "\"X_type\":\"" + m.X_type + "\",\"Y_type\":\"" + m.Y_type + "\",\"Goodness\":\"" + m.Goodness + "\","
                + "\"Intercept\":\"" + m.Intercept + "\",\"Coefficient\":\"" + m.Coefficient + "\"}";
            else
                resultJson = "{\"Distid\":\"1\",\"Mdate\":\"2009-01-01\","
                + "\"Module_typeid\":\"1\",\"Module_name\":\"长势监测\",\"Model_type\":\"linear\","
                + "\"X_type\":\"NDVI\",\"Y_type\":\"LAI\",\"Goodness\":\"0.6\","
                + "\"Intercept\":\"1.2\",\"Coefficient\":\"1.2\"}";
        }

        Response.Write(resultJson);
    }
}
